<?php
/**
 * ajax分页处理类. 前台显示分页条需包含jquery文件.
 * @package base
 * @author 黎湘 <lixiang3@staff.sina.com.cn>
 * @version 1.0
 * @copyright (c) 2010 All rights reserved.
 * @example ./ajax_page_bar.class.php 查看源代码
 * @example ./ajax_page_bar.example.php 如何使用请点这里
 */

class ajax_page_bar
{
    private $_navigation_item_count 	= 5;    			//导航栏显示导航总页数
    private $_page_size 				= null;             //每页项目数
    private $_align 					= "right";          //导航栏显示位置
    private $_item_count 				= null;             //总项目数
    private $_page_count 				= null;             //总页数
    private $_cur_page 					= null;             //当前页
    private $_page_para_name 			= "p";           	//页号参数名称

    private $_first_page_string 		= "|<<";            //导航栏中第一页显示的字符
    private $_next_page_string 			= ">>";             //导航栏中前一页显示的字符
    private $_prev_page_string 			= "<<";            	//导航栏中后一页显示的字符
    private $_last_page_string 			= ">>|";            //导航栏中最后一页显示的字符
    private $_split_string 				= " | ";            //页数字间的间隔符 
    private $_this_url 					= "";               //当前页完整url
    private $_base_url 					= "";               //当前页的基本url,不包含?号以及后面的参数
    private $_url_params 				= array();          //当前页面的url?号后面的参数.

    public function __construct($item_count, $page_size)
    {
        $this->_this_url = $this->get_this_url();      //当前完整url信息,包括参数.

        if(!is_numeric($item_count) || (!is_numeric($page_size)))
        {
        	throw new Exception("Pagination Error:not Number");
		}
        $this->_item_count = $item_count;
        $this->_page_size = $page_size;

        $this->_page_count = ceil($item_count/$page_size);            //总页数

        $url_params = $this->get_url_params($this->_this_url);
        $this->_base_url = $url_params[0];

        $page = $url_params[1][$this->_page_para_name];
        if(empty($page) || (!is_numeric($page)))    //为空或不是数字，设置当前页为1
        {
            $this->_cur_page = 1;
        }
        else
        {
            if($page < 1)
                $page = 1;
            if($page > $this->_page_count)
                $page = $this->_page_count;
            $this->_cur_page = $page;
        }
    }

    /**
     * 返回当前页
     * @return int 当前页
     */
    public function get_cur_page()
    {
        return $this->_cur_page;
    }

    /**
     * 返回页数
     * @return int 页数
     */
    public function get_total_page()
    {
        return $this->_page_count;
    }

    /**
     * 返回导航栏目
     * @return string 导航html                class="PageNavigation"
     */
    public function get_navigation($showid)
    {
    	$showid = htmlspecialchars($showid);
        $navigation = '<div style="text-align:'.$this->_align.'">';

        $page_cote = ceil($this->_cur_page / ($this->_navigation_item_count - 1)) - 1;    //当前页处于第几栏分页
        $page_cote_count = ceil($this->_page_count / ($this->_navigation_item_count - 1));    //总分页栏
        $page_start = $page_cote * ($this->_navigation_item_count -1) + 1;                    //分页栏中起始页
        $page_end = $page_start + $this->_navigation_item_count - 1;                        //分页栏中终止页
        if($this->_page_count < $page_end)
        {
            $page_end = $this->_page_count;
        }
                $navigation .= "总共：{$this->_item_count}条　{$this->_page_count}页\n";
        if($page_cote > 0)                                //首页导航
        {
            $navigation .= '<a href="javascript:;" onclick="gotoPage_'.$showid.'(1); return false;">'.($this->_first_page_string).'</a>';
        }
        if($this->_cur_page != 1)                    //上一页导航
        {
            $navigation .= '<a href="javascript:;" onclick="gotoPage_'.$showid.'('.($this->_cur_page-1).'); return false;"';
            $navigation .= ">$this->_prev_page_string</a> ";
        }
        while ($page_start <= $page_end)                    //构造数字导航区
        {
            if($page_start==$this->_page_count){                                //去掉最后一页数字后边的分隔符
                    $this->_split_string = "  ";
                }
            if($page_start == $this->_cur_page)
            {
                $navigation .= "<strong>$page_start</strong>".$this->_split_string;
            }
            else
            {
                $navigation .= '<a href="javascript:;" onclick="gotoPage_'.$showid.'('.($page_start).'); return false;">'.$page_start.'</a>'.$this->_split_string;
            }
            $page_start++;
        }
        if($this->_cur_page != $this->_page_count)    //下一页导航
        {
            $navigation .= '<a href="javascript:;" onclick="gotoPage_'.$showid.'('.($this->_cur_page+1).'); return false;">'.$this->_next_page_string.'</a>';
        }
        if($page_cote < $page_cote_count-1)                //未页导航
        {
            $navigation .= '<a href="javascript:;" onclick="gotoPage_'.$showid.'('.($this->_page_count).'); return false;">'.$this->_last_page_string.'</a>';
        }
        
        
        //添加直接导航框
/*        $navigation .= '  &nbsp;&nbsp;<select onchange="gotoPage_'.$showid.'(this.options[this.selectedIndex].value);">';
        for ($i=1;$i<=$this->_page_count;$i++){
                if ($this->get_cur_page()==$i){
                        $selected = "selected";
                }
                else {
                        $selected = "";
                }
                $navigation .= '<option value='.$i.' '.$selected.'>'.$i.'</option>';
        }
        $navigation .= '</select>';
*/        
        
        $navigation .= "</div>";

        $ajax_url = htmlspecialchars($this->_base_url);
        $page_para_name = $this->_page_para_name;
        $ajax_pas = "";
        foreach($this->_url_params as $pas=>$val){
            $ajax_pas .= $pas."=".$val."&";
        }
        $ajax_pas = htmlspecialchars(rtrim($ajax_pas,"&"));
        
        $navigation .= "<script charset='utf8'>";
        $navigation .= <<<PAGERJS
            function gotoPage_$showid(page){
                $.ajax({type:"GET",url:"$ajax_url",data:"$page_para_name="+page+"&"+"$ajax_pas",beforeSend:function(){
                	if(typeof(show_server_before)=='function')
                		show_server_before('$showid');
                },success:function(data){
                	show_server_data(data,'$showid');
                },error:function(msg){
                	if(typeof(show_server_error)=='function')
                		show_server_error(msg,'$showid');
                }
                });
                return false;
            }
PAGERJS;
        $navigation .= "</script>";
        return $navigation;
    }


    /**
     * 取得导航栏显示导航总页数
     *
     * @return int 导航栏显示导航总页数
     */
    public function get_navigation_item_count()
    {
        return $this->_navigation_item_count;
    }

    public function set_cur_page($cur_page)
    {
    	$this->_cur_page = $cur_page;
    }

    public function set_url_params($params)
    {
        $this->_url_params = $params;
    }

    /**
     * 设置导航栏显示导航总页数
     *
     * @param  int $navigationCount:导航栏显示导航总页数
     */
    public function set_navigation_item_count($navigation_count)
    {
        if(is_numeric($navigation_count))
        {
            $this->_navigation_item_count = $navigation_count;
        }
    }

    /**
     * 设置首页显示字符
     * @param string $firstPageString 首页显示字符
     */
    public function set_first_page_string($first_page_string)
    {
        $this->_first_page_string = $first_page_string;
    }

    /**
     * 设置上一页导航显示字符
     * @param string $previousPageString:上一页显示字符
     */
    public function set_prev_page_string($prev_page_string)
    {
        $this->_prev_page_string = $prev_page_string;
    }

    public function set_base_url($base_url)
    {
        $this->_base_url = $base_url;
    }

    /**
     * 设置下一页导航显示字符
     * @param string $nextPageString:下一页显示字符
     */
    public function set_next_page_string($next_page_string)
    {
        $this->_next_page_string = $next_page_string;
    }

    /**
     * 设置未页导航显示字符
     * @param string $nextPageString:未页显示字符
     */
    public function set_last_page_string($last_page_string)
    {
        $this->_last_page_string = $last_page_string;
    }

    /**
     * 设置导航字符显示位置
     * @param string $align:导航位置
     */
    public function set_align($align)
    {
        $align = strtolower($align);
        if($align == "center")
        {
            $this->_align = "center";
        }elseif($align == "right")
        {
            $this->_align = "right";
        }else
        {
            $this->_align = "left";
        }
    }
    /**
     * 设置页面参数名称
     * @param string $pageParamName:页面参数名称
     */
    public function set_page_param_name($page_param_name)
    {
        $this->_page_para_name = $page_param_name;
    }

    /**
     * 获取页面参数名称
     * @return string 页面参数名称
     */
    public function get_page_param_name()
    {
        return $this->_page_para_name;
    }

    /**
     * 生成导航链接地址
     * @param int $targetPage:导航页
     * @return string 链接目标地址
     */
    public function create_href($target_page = null)
    {

        $url_and_params = $this->get_url_params($this->_this_url);

        $target_url = $url_and_params[0];  //url

        $params = $url_and_params[1];     //参数

        $targetUrl .= "?";

        foreach ($params as $key => $value)
        {

            if($key != $this->_page_para_name && $key!="")
            {
                $target_url .= "$key=$value&";
            }
        }
        if(isset($target_page))                //指定目标页
            $target_url .= $this->_page_para_name."=".$target_page;
        else
            $target_url .= $this->_page_para_name."=";
        return $target_url;
    }

    /**
     * 获取当前完整URL,包括参数
     * @return string
     */
    private function get_this_url()
    {
        return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !='off')?'https':'http'.'://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
    }

    /**
     * 获取网址,和后面的参数.
     * @param string $url
     * @return array
     */
    private function get_url_params($url)
    {
        $result = array();
        $url_params = explode("?",$url,2);
        $result[0] = $url_params[0];             //url

        $every_params = array();
        $params = explode("&",$url_params[1]);
        foreach($params as $str){
            $element = explode("=",$str);
            $every_params[$element[0]] = $element[1];
        }

        $result[1] = $every_params;
        unset($params);
        unset($every_params);

        return $result;

    }
}
